﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WikiPediaPlugin
{
    public partial class frmMain : Form
    {
        PluginSettings m_settings;
        
        public frmMain(string origlink, PluginSettings settings)
        {
            m_settings = settings;
            InitializeComponent();
            foreach (string name in m_settings.languagenames)
            {
                cbxLanguage.Items.Add(name);
            }
            cbxLanguage.SelectedIndex = m_settings.LanguageNumber;
            txbxText.Text = origlink;
            if (!string.IsNullOrEmpty(origlink))
                txbxText_TextChanged(this, null);
            
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.FileName = "http://en.wikipedia.org/";
            process.Start();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txbxWikiLink.Text))
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.UseShellExecute = true;
                process.StartInfo.FileName = txbxWikiLink.Text;
                process.Start();
            }
        }

        private void txbxText_TextChanged(object sender, EventArgs e)
        {
            string wiki = m_settings.WikiCode(txbxText.Text);
            string lang = m_settings.languagecodes[cbxLanguage.SelectedIndex];
            txbxWikiLink.Text = string.Format("http://{0}.wikipedia.org/wiki/{1}", lang, wiki);
        }

        public string Link
        {
            get
            {
                string link = string.Format("<a href=\"{0}\" target=\"_blank\">{1}</a>", txbxWikiLink.Text, LinkText);
                return link;
            }
        }

        public string LinkText
        {
            get { return txbxText.Text; }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(LinkText))
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("You haven't entered any text to create a link from", "Big Fat Hair Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cbxLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            txbxText_TextChanged(this, null);
        }
    }
}
